import request from '@/utils/request'

// 获取聊天机器人对话列表
export function getChatBotChatList(args) {
  return request({
    url: `/chatbot/api/chatbot-chat/${args && ('?' + args) || ''}`,
    method: 'get'
  })
}

// 创建聊天机器人对话
export function createChatBotChat(data) {
  return request({
    url: '/chatbot/api/chatbot-chat/',
    headers: { 'content-type': 'application/json' },
    method: 'post',
    data: data
  })
}

// 获取聊天机器人对话详情
export function getChatBotChatDetail(id) {
  return request({
    url: `/chatbot/api/chatbot-chat/${id}/`,
    method: 'get'
  })
}

// 更新聊天机器人对话
export function updateChatBotChat(id, data) {
  return request({
    url: `/chatbot/api/chatbot-chat/${id}/`,
    headers: { 'content-type': 'application/json' },
    method: 'put',
    data: data
  })
}

// 删除聊天机器人对话
export function deleteChatBotChat(id) {
  return request({
    url: `/chatbot/api/chatbot-chat/${id}/`,
    method: 'delete'
  })
}

// 根据对话ID获取聊天记录 (对话ID格式: xxx@c.us 或 xxx@g.us)
export function getChatBotChatByConversationId(conversationId, args) {
  return request({
    url: `/chatbot/api/chatbot-chat/conversation/${encodeURIComponent(conversationId)}/${args && ('?' + args) || ''}`,
    method: 'get'
  })
}

// 创建或关联聊天机器人对话到指定对话ID
export function createChatBotChatWithConversationId(conversationId, data) {
  return request({
    url: `/chatbot/api/chatbot-chat/conversation/${encodeURIComponent(conversationId)}/`,
    headers: { 'content-type': 'application/json' },
    method: 'post',
    data: data
  })
}

// 根据对话ID获取最近的聊天记录
export function getRecentChatByConversationId(conversationId, limit = 10) {
  return request({
    url: `/chatbot/api/chatbot-chat/conversation/${encodeURIComponent(conversationId)}/recent/?limit=${limit}`,
    method: 'get'
  })
}

// 批量获取多个对话ID的聊天记录
export function getBatchChatsByConversationIds(conversationIds) {
  return request({
    url: '/chatbot/api/chatbot-chat/conversation/batch/',
    headers: { 'content-type': 'application/json' },
    method: 'post',
    data: { conversation_ids: conversationIds }
  })
}

// 根据对话ID更新聊天状态
export function updateChatStatusByConversationId(conversationId, status) {
  return request({
    url: `/chatbot/api/chatbot-chat/conversation/${encodeURIComponent(conversationId)}/status/`,
    headers: { 'content-type': 'application/json' },
    method: 'put',
    data: { status: status }
  })
}

// 获取用户类型的对话列表 (xxx@c.us)
export function getUserConversations(args) {
  return request({
    url: `/chatbot/api/chatbot-chat/conversations/users/${args && ('?' + args) || ''}`,
    method: 'get'
  })
}

// 获取群组类型的对话列表 (xxx@g.us)
export function getGroupConversations(args) {
  return request({
    url: `/chatbot/api/chatbot-chat/conversations/groups/${args && ('?' + args) || ''}`,
    method: 'get'
  })
}
